package com.xbongbong.paas.script.controller;

import com.xbongbong.crm.service.SearchCustomerService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.SearchCustomerCompanyModel;
import com.xbongbong.paas.model.SearchCustomerUserModel;
import com.xbongbong.paas.pojo.dto.FixDataJobDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.SearchCustomerUserEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xinpeng.jiang
 * @version V1.0
 * @date 2020/12/4 10:39
 * @since V1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/searchCustomer")
public class SearchCustomerScriptController {
    private static final Logger LOG = LoggerFactory.getLogger(SearchCustomerScriptController.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private SearchCustomerCompanyModel searchCustomerCompanyModel;
    @Resource
    private SearchCustomerUserModel searchCustomerUserModel;
    @Resource
    private UserModel userModel;
    @Resource
    private SearchCustomerService searchCustomerService;

    @RequestMapping(value = "/userLeave", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userLeave(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        long start = DateTimeUtil.getInt();
        List<String> corpidList = fixDataJobDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            corpidList = searchCustomerCompanyModel.getCorpidList();
        }
        if (CollectionsUtil.isEmpty(corpidList)) {
            return "没有需要更新的数据";
        }
        LOG.warn("数据库中共有公司{}家", corpidList.size());
        int corpidCount = 1;

        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> searchCustomerParam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.DEL, DelEnum.DELETE.getDel());
        searchCustomerParam.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        for (String corpid : corpidList) {
            LOG.warn(corpidCount + "/" + corpidList.size());
            corpidCount++;
            // 查询离职员工
            param.put(StringConstant.CORPID, corpid);
            List<String> userIds = userModel.getUserIds(param);
            if (CollectionsUtil.isEmpty(userIds)) {
                continue;
            }
            // 处理搜客未解绑的账号
            searchCustomerParam.put(StringConstant.CORPID, corpid);
            searchCustomerParam.put("userIdIn", userIds);
            List<SearchCustomerUserEntity> list = searchCustomerUserModel.findEntitys(searchCustomerParam);
            if (CollectionsUtil.isEmpty(list)) {
                continue;
            }
            for (SearchCustomerUserEntity searchCustomerUserEntity : list) {
                searchCustomerService.removeSoukePosition(corpid, searchCustomerUserEntity.getUserId());
            }
        }

        LOG.warn("consume:{}", (DateTimeUtil.getInt() - start));
        return "操作成功";
    }

}
